package com.blb.eduorderservice.controller;

import com.blb.common.entity.UserCourseOrder;
import com.blb.eduorderservice.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 */
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 插入订单
     * @param order
     * @return
     */
    @PostMapping("/order")
    public ResponseEntity<UserCourseOrder> makeOrder(@RequestBody UserCourseOrder order){
        orderService.saveOrder(order);
        return ResponseEntity.ok(order);
    }

    /**
     * 修改订单状态
     * @param order
     * @return
     */
    @RequestMapping("/order/status")
    public ResponseEntity<UserCourseOrder> changeOrderStatus(@RequestBody UserCourseOrder order){
        UserCourseOrder userCourseOrder = orderService.changeOrderStatus(order.getTradeNo(), order.getStatus());
        return ResponseEntity.ok(userCourseOrder);
    }

    /**
     * 查询订单
     * @param tradeNo
     * @return
     */
    @RequestMapping("/order/tradeNo")
    public ResponseEntity<UserCourseOrder> getOrderByTradeNo(@RequestBody String tradeNo){
        UserCourseOrder userCourseOrder = orderService.getOrderByTradeNo(tradeNo);
        return ResponseEntity.ok(userCourseOrder);
    }

    /**
     * 查询用户所有订单
     * @param userId
     * @return
     */
    @GetMapping("/orders")
    public ResponseEntity<List<UserCourseOrder>> findUserOrders(long userId){
        List<UserCourseOrder> orders = orderService.findUserOrders(userId);
        return ResponseEntity.ok(orders);
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("/order/{orderId}")
    public ResponseEntity<String> deleteOrder(@PathVariable("orderId") long orderId){
        orderService.deleteOrder(orderId);
        return ResponseEntity.ok("ok");
    }
}
